<?php

namespace app\astrologer\controller\v4;


use app\astrologer\controller\AstrAuth;
use app\common\cachemodel\ZlcrAstrAccount;
use app\common\cachemodel\ZlcrAstrExerciseRecord;
use app\common\cachemodel\ZlcrAstrNotAnswerQuestion;
use app\common\cachemodel\ZlcrSlide;
use think\Env;

/**
 * 占星师后台首页相关
 * Class Index
 * @support v2.0.0
 * @package app\astrologer\controller
 */
class Index extends AstrAuth
{
    /**
     * 占星师后台首页接口
     * @throws \Exception
     */
    public function index()
    {
        $astrAccountInfo = (new ZlcrAstrAccount())->getAstrAccountInfo($this->astrInfo['id']);
        $notAnswerQids = (new ZlcrAstrNotAnswerQuestion())->getNotAnswerQids($this->astrInfo['id']); // 获取弃答问题ID
        $pauseTime = $this->astrInfo['pause_time'] - time();
        $authStatus = $this->astrInfo['auth_status'];
        $status = 1;
        $authList = [];
        if ($authStatus['custom_report'] !== 1)
        {
            $imgHost = Env::get('host.photo');
            $astrAnswerModel = new ZlcrAstrExerciseRecord();
            $astrAnswerInfo = $astrAnswerModel->getExerciseInfo($this->astrInfo['id'], 1);   // 1=>定制报告 2=>问答三合一 3=>问答星盘 4=>问答骰子 5=>问答塔罗
            if ($astrAnswerInfo && (int)$astrAnswerInfo['status'] === 2 && $this->request->time() - $astrAnswerInfo['fail_time'] > 86400)
            {
                $astrAnswerInfo = null; // 失败24小时后允许重新考试
            }
            $res = $astrAnswerModel->getExerciseRes2($astrAnswerInfo);
            $customReport1Status = $res[1]['status']; // 考试状态
            $customReport1Tips = $res[1]['tips']; //
            $customReport2Status = $res[2]['status']; // 考试状态
            $customReport2Tips = $res[2]['tips']; //
            $status = 0;
            $authList[] = [
                'title' => '定制报告技能测试考核',
                'list' => [
                    [
                        'type' => 1,
                        'img' => $imgHost . '/mini/astrologer/test/5.png',
                        'name' => '占星师测评-定制报告',
                        'desc' => '您需要熟练掌握星盘相关知识，能独立输出自己的观点。',
                        'tips' => '通过后获得定制报告及星盘问答接单资格',
                        'is_passed' => $authStatus['custom_report'] === 1 ? 1 : 0,
                        'list' => [
                            [
                                'id' => 1,
                                'title' => '选择题',
                                'status' => $customReport1Status ?? 2,
                                'tips' => $customReport1Tips ?? '',
                                'content' => [
                                    '选择题考核共40题，分单选及多选，答对35题则考核成功进入基础面试考核',
                                    '选择题考核每题回答时间为60秒，若超时则当前题默认0分，自动进入下一道题',
                                    '选择题考核总时长为1小时，若超过规定时间未提交则所有超时的题，自动设置为0分',
                                    '考核如不通过则从考试结束时间开始计算间隔24小时才可继续重新考核'
                                ],
                            ],
                            [
                                'id' => 3,
                                'title' => '基础面试',
                                'status' => $customReport2Status ?? 2,
                                'tips' => $customReport2Tips ?? '',
                                'content' => [],
                            ],
                        ],
                    ],
                ],
            ];
        }

        $data = [
            'is_show_send_order' => 0,//是否展示派单模式部分（默认不展示，因为只有之前的准了App有定制报告的派单模式，但是没有上，这里做成动态的）
            'userData' => [
                'avatar' => $this->astrInfo['avatar'],// 占星师头像
                'astroName' => $this->astrInfo['nickname'],// 占星师名称
                'astroLevel' => (int)$this->astrInfo['astr_level'],// 0=>专业占星师,1=>高级占星师,2=>大师占星师,3=>至臻占星师
                'astroGrade' => $this->astrInfo['astro_grade'],// 占星师能力分，初始分数为100
                'discardTimes' => 3 - count($notAnswerQids),// 占星师今日剩余弃答次数，默认每天有三次弃答机会
                'lastDiscardTime' => '',//今日，上次弃答时间（弃答间隔时间为15分钟，每天重置）【前端已去掉】
                'monthEarnings' => $astrAccountInfo['month_earnings'] ?? 0,// 本月收益 单位/元
                'notGetEarnings' => $astrAccountInfo['last_month_earnings'],// 未打款 单位/元
                'ruleUrl' => Env::get('host.domain') . '/web/rule_astrologer',// 占星师 能力分及等级细则 url
                'pause_status' => $pauseTime > 0 ? 1 : 0, // 账户临时冻结状态 到时间自动解冻 1=>账号冻结 0=>未冻结
                'pause_time' => $pauseTime > 0 ? $pauseTime : 0, // 距离解冻剩余时间的 时间戳
                'auth_status' => $status,
                'auth_list' => $authList,
                'report_assign_task_status' => $this->astrInfo['custom_report_status'] ?? 0
            ],
            'bannerList' => (new ZlcrSlide())->getSlideList(false),
        ];

        return show(config('code.success'), '请求成功', $data);
    }
}
